﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace GestorDeFlotasDesktop.AbmAuto
{
    public partial class AbmAuto : Form
    {
        private Modelo.FormActions.Actions action = Modelo.FormActions.Actions.Alta;
        private int selected_elem = -1;

        public AbmAuto(Modelo.FormActions.Actions runAction, int id)
        {
            InitializeComponent();

            SqlConnection con = Modelo.Modelo.createConnection();
            SqlDataAdapter da = new SqlDataAdapter(Modelo.SqlQueries.getMarcasTaxi(), con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            marca_cb.DataSource = ds.Tables[0].DefaultView;
            marca_cb.DisplayMember = "Marca";
            marca_cb.ValueMember = "Id";
            marca_cb.SelectedIndex = -1;
            Modelo.Modelo.closeConnection(con);

            SqlConnection con2 = Modelo.Modelo.createConnection();
            SqlDataAdapter da2 = new SqlDataAdapter(Modelo.SqlQueries.getMarcaModeloReloj(), con2);
            DataSet ds2 = new DataSet();
            da2.Fill(ds2);
            reloj_cb.DataSource = ds2.Tables[0].DefaultView;
            reloj_cb.DisplayMember = "Marca";
            reloj_cb.ValueMember = "Id";
            reloj_cb.SelectedIndex = -1;
            Modelo.Modelo.closeConnection(con2);

            switch (runAction)
            {
                case Modelo.FormActions.Actions.Baja:
                    selected_elem = id;
                    this.loadData(selected_elem);
                    action = Modelo.FormActions.Actions.Baja;
                    marca_cb.Enabled = false;
                    reloj_cb.Enabled = false;
                    tb_licencia.Enabled = false;
                    tb_modelo.Enabled = false;
                    tb_patente.Enabled = false;
                    tb_rodado.Enabled = false;
                    cb_hab.Enabled = false;
                    aceptar.Text = "Eliminar";
                    break;
                case Modelo.FormActions.Actions.Modificacion:
                    selected_elem = id;
                    this.loadData(selected_elem);
                    action = Modelo.FormActions.Actions.Modificacion;
                    aceptar.Text = "Modificar";
                    break;
            }

        }

        public AbmAuto(Modelo.FormActions.Actions runAction)
        {
            InitializeComponent();

            SqlConnection con = Modelo.Modelo.createConnection();
            SqlDataAdapter da = new SqlDataAdapter(Modelo.SqlQueries.getMarcasTaxi(), con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            marca_cb.DataSource = ds.Tables[0].DefaultView;
            marca_cb.DisplayMember = "Marca";
            marca_cb.ValueMember = "Id";
            marca_cb.SelectedIndex = -1;
            Modelo.Modelo.closeConnection(con);

            SqlConnection con2 = Modelo.Modelo.createConnection();
            SqlDataAdapter da2 = new SqlDataAdapter(Modelo.SqlQueries.getMarcaModeloReloj(), con2);
            DataSet ds2 = new DataSet();
            da2.Fill(ds2);
            reloj_cb.DataSource = ds2.Tables[0].DefaultView;
            reloj_cb.DisplayMember = "Marca";
            reloj_cb.ValueMember = "Id";
            reloj_cb.SelectedIndex = -1;
            Modelo.Modelo.closeConnection(con2);

            cb_hab.Enabled = false;
        }

        private void loadData(int id)
        {
            using (SqlConnection con3 = Modelo.Modelo.createConnection())
            {
                Modelo.Modelo.openConnection(con3);
                SqlCommand comando_get_autos = new SqlCommand(Modelo.SqlQueries.getAutoById(selected_elem), con3);
                SqlDataReader reader = comando_get_autos.ExecuteReader();
                while (reader.Read())
                {
                    marca_cb.SelectedValue= Convert.ToInt32(reader["Marca"]);
                    reloj_cb.SelectedValue = Convert.ToInt32(reader["Reloj"]);
                    tb_licencia.Text = reader["Licencia"].ToString();
                    tb_modelo.Text = reader["Modelo"].ToString();
                    tb_patente.Text = reader["Patente"].ToString();
                    tb_rodado.Text = reader["Rodado"].ToString();
                    cb_hab.Checked = (Boolean)reader["Habilitado"];
                }
            }
        }

        private void aceptar_Click_1(object sender, EventArgs e)
        {
            switch (action)
            {
                case Modelo.FormActions.Actions.Alta:
                    string error_txt = "ERROR: ";
                    if (String.IsNullOrEmpty(marca_cb.Text))
                    {
                        error_txt = String.Concat(error_txt, "\n Marca es obligatorio");
                    }
                    if (String.IsNullOrEmpty(reloj_cb.Text))
                    {
                        error_txt = String.Concat(error_txt, "\n Reloj es obligatorio");
                    }
                    if (String.IsNullOrEmpty(tb_licencia.Text))
                    {
                        error_txt = String.Concat(error_txt, "\n Licencia es obligatorio");
                    }
                    if (String.IsNullOrEmpty(tb_modelo.Text))
                    {
                        error_txt = String.Concat(error_txt, "\n Modelo es obligatorio");
                    }
                    if (String.IsNullOrEmpty(tb_patente.Text))
                    {
                        error_txt = String.Concat(error_txt, "\n Patente es obligatorio");
                    }
                    if (String.IsNullOrEmpty(tb_rodado.Text))
                    {
                        error_txt = String.Concat(error_txt, "\n Rodado es obligatorio");
                    }
                    if((!String.IsNullOrEmpty(marca_cb.Text)) && (!String.IsNullOrEmpty(reloj_cb.Text))&& (!String.IsNullOrEmpty(tb_licencia.Text))&& (!String.IsNullOrEmpty(tb_modelo.Text))&& (!String.IsNullOrEmpty(tb_patente.Text))&& (!String.IsNullOrEmpty(tb_rodado.Text)))
                    {
                        using (SqlConnection con_un = Modelo.Modelo.createConnection())
                        {
                            try
                            {
                                Modelo.Modelo.openConnection(con_un);
                                SqlCommand comando_check_unique = new SqlCommand(Modelo.SqlQueries.getTaxiMellizo(tb_patente.Text, tb_licencia.Text, Convert.ToInt32(reloj_cb.SelectedValue)), con_un);
                                SqlDataReader reader = comando_check_unique.ExecuteReader();
                                if (reader.Read())
                                {
                                    MessageBox.Show("Taxi mellizo");
                                    break;
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
                        }

                        using (SqlConnection con_un2 = Modelo.Modelo.createConnection())
                        {
                            try
                            {
                                Modelo.Modelo.openConnection(con_un2);
                                SqlCommand comando_check_unique2 = new SqlCommand(Modelo.SqlQueries.getRelojUsado(Convert.ToInt32(reloj_cb.SelectedValue)), con_un2);
                                SqlDataReader reader = comando_check_unique2.ExecuteReader();
                                if (reader.Read())
                                {
                                    MessageBox.Show("Reloj en uso");
                                    break;
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
                        }

                        using (SqlConnection con = Modelo.Modelo.createConnection())
                        {
                            try
                            {
                                Modelo.Modelo.openConnection(con);
                                SqlCommand comando_add_user = new SqlCommand(Modelo.SqlQueries.addAuto(
                                                            Convert.ToInt32(marca_cb.SelectedValue),
                                                            Convert.ToInt32(reloj_cb.SelectedValue),
                                                            tb_licencia.Text,
                                                            tb_modelo.Text,
                                                            tb_patente.Text,
                                                            tb_rodado.Text,
                                                            true), con);
                                comando_add_user.ExecuteNonQuery();
                                MessageBox.Show("Taxi agregado");
                                Login.Login.mainForm.openForm(new AbmAuto(Modelo.FormActions.Actions.Alta));
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
                        }
                    }else {
                        MessageBox.Show(error_txt);
                    }
                    break;
                case Modelo.FormActions.Actions.Baja:
                    using (SqlConnection con = Modelo.Modelo.createConnection())
                    {
                        try
                        {
                            Modelo.Modelo.openConnection(con);
                            SqlCommand comando = new SqlCommand(Modelo.SqlQueries.deleteAuto(selected_elem), con);
                            comando.ExecuteNonQuery();
                            MessageBox.Show("Taxi eliminado");
                            Login.Login.mainForm.openForm(new AbmAuto(Modelo.FormActions.Actions.Alta));
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                    break;
                case Modelo.FormActions.Actions.Modificacion:
                    using (SqlConnection con_un = Modelo.Modelo.createConnection())
                    {
                        try
                        {
                            Modelo.Modelo.openConnection(con_un);
                            SqlCommand comando_check_unique = new SqlCommand(Modelo.SqlQueries.getTaxiMellizo(tb_patente.Text, tb_licencia.Text, Convert.ToInt32(reloj_cb.SelectedValue)), con_un);
                            SqlDataReader reader = comando_check_unique.ExecuteReader();
                            if (reader.Read())
                            {
                                MessageBox.Show("Taxi mellizo");
                                break;
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                    using (SqlConnection connection = Modelo.Modelo.createConnection())
                    {
                        try
                        {
                            Modelo.Modelo.openConnection(connection);
                            SqlCommand comando = new SqlCommand(Modelo.SqlQueries.updateAuto(selected_elem,
                                                        Convert.ToInt32(marca_cb.SelectedValue),
                                                        Convert.ToInt32(reloj_cb.SelectedValue),
                                                        tb_licencia.Text,
                                                        tb_modelo.Text,
                                                        tb_patente.Text,
                                                        tb_rodado.Text,
                                                        cb_hab.Checked), connection);
                            comando.ExecuteNonQuery();
                            MessageBox.Show("Taxi modificado");
                            Login.Login.mainForm.openForm(new AbmAuto(Modelo.FormActions.Actions.Alta));
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                    break;
            }
        }
    }
}
